package cn.fourhundredandeighteen.view.StudentView.StudentFunction;


import cn.fourhundredandeighteen.UserInfo;
import cn.fourhundredandeighteen.controller.StudentController;
import cn.fourhundredandeighteen.entity.Student;
import cn.fourhundredandeighteen.utill.NumberUtil;
import cn.fourhundredandeighteen.utill.Result;

import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class StudentInfo {
    private final StudentController studentController = new StudentController();
    private final Result<Student> studentResult = studentController.studentInfo(UserInfo.Session);
    private final Student studentData = studentResult.getData();//获取学生数组数据

    public Map<Integer, String[]> getstudentInfo() {   //学生个人信息获取
        // TODO add your code here

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<Integer, String[]> integerStringHashMap = new HashMap<>();
        //获取字段
        String[] Lablename = {"学号:", "名字:", "密码:", "性别:", "年龄:", "专业:", "班级:", "更新时间:"};
        //获取数据

        String[] studentdata = {
                String.valueOf(studentData.getStudentId()),
                String.valueOf(studentData.getStudentName()),
                String.valueOf(studentData.getStudentPassword()),
                String.valueOf(studentData.getStudentGender()),
                String.valueOf(studentData.getStudentAge()),
                String.valueOf(studentData.getStudentMajor()),
                String.valueOf(studentData.getStudentClass()),
                dateFormat.format(studentData.getUpdateTime())
        };
        integerStringHashMap.put(1, Lablename);//数据键值对
        integerStringHashMap.put(2, studentdata);
        return integerStringHashMap;
    }

    public String setStudentInfo(ArrayList<JTextField> Data) {   //学生个人信息获取
        // TODO add your code here
        if (!NumberUtil.Password(String.valueOf(Data.get(2).getText()))) {
            return "请输入6位数字密码";
        }
        if (NumberUtil.Age(String.valueOf(Data.get(4).getText()))) {
            return "请输入正确年龄";
        }
//                if (NumberUtil.Major(String.valueOf(Data.get(3).getText()))){
//                        return "请输入正确的性别";
//                }


        //获取需要上传的数据并保存至数组
        Date date = new Date();
        studentData.setStudentPassword(String.valueOf(Data.get(2).getText()));
        studentData.setStudentGender(String.valueOf(Data.get(3).getText()));
        studentData.setStudentAge(Integer.valueOf(String.valueOf(Data.get(4).getText())));
        studentData.setStudentMajor(String.valueOf(Data.get(5).getText()));
        studentData.setStudentClass(String.valueOf(Data.get(6).getText()));
        studentData.setUpdateTime(date);
        try {
            Result result = studentController.upDataStudentInfo(studentData);
            System.out.println(result);
            if (result.getMassage() == "SUCCESS") {
                return "修改成功";

            } else {
                return "修改失败";
            }
        } catch (NumberFormatException numberFormatException) {
            return "请输入正确的参数";
        }

    }

}